package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.Banner;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.BannerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerServices bannerServices;

    /**
     * 添加banner
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addBanner(@RequestBody String data, HttpSession session){
        Banner banner = JsonStringUtil.readValue(data, Banner.class);
        UserSessionInfo userSessionInfo = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        if(userSessionInfo==null){
            return new JsonResult().Serailze(new ResultModel(false,ErrorCode.LOGIN_USER));
        }
        banner.setUserId(userSessionInfo.getLoginUser().getId());
        ResultModel result= bannerServices.doAdd(banner);
        return new JsonResult().Serailze(result);
    }

    /**
     * 分页查询全部Banner
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String queryBannerList(@RequestBody String data){
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel= bannerServices.queryBannersList(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * banner启用
     */
    @RequestMapping(value = "/set/enable",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String setEnable(@RequestBody String data){
        Banner banner=JsonStringUtil.readValue(data,Banner.class);
        ResultModel resultModel= bannerServices.doSetEnable(banner.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * banner禁用
     */
    @RequestMapping(value = "/set/disable",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String setDisable(@RequestBody String data){
        Banner banner=JsonStringUtil.readValue(data,Banner.class);
        ResultModel resultModel= bannerServices.doSetDisable(banner.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * banner的置顶
     */
    @RequestMapping(value = "/set/top",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String setIsTop(@RequestBody String data){
        Banner banner=JsonStringUtil.readValue(data,Banner.class);
        ResultModel resultModel= bannerServices.doSetIsTop(banner.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * banner的取消置顶
     */
    @RequestMapping(value = "/set/not/top",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String setNotTop(@RequestBody String data){
        Banner banner=JsonStringUtil.readValue(data,Banner.class);
        ResultModel resultModel= bannerServices.doSetNotTop(banner.getId());
        return new JsonResult().Serailze(resultModel);
    }


    /**
     * APP端查询所有banner
     * @return
     */
    @RequestMapping(value = "/app/list",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String queryAll(){
        List<Banner> banners = bannerServices.queryAllForApp();
        ResultModel resultModel = new ResultModel();
        resultModel.setBresult(true);
        resultModel.setObject(banners);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 平台删除banner
     * @param data
     * @return
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String delete(@RequestBody String data){
        Banner banner = JsonStringUtil.readValue(data, Banner.class);
        ResultModel resultModel = bannerServices.doDelete(banner.getId());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 平台banner编辑
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String update(@RequestBody String data){
        Banner banner = JsonStringUtil.readValue(data, Banner.class);
        ResultModel resultModel = bannerServices.doUpdate(banner);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 根据ID查询指定banner
     * @param data
     * @return
     */
    @RequestMapping(value = "/query/id",method = {RequestMethod.POST},produces = "application/json")
    @ResponseBody
    public String query(@RequestBody String data){
        Banner banner = JsonStringUtil.readValue(data, Banner.class);
        Banner dbBanner = bannerServices.queryBanner(banner.getId());
        ResultModel resultModel = new ResultModel();
        if(dbBanner==null){
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.BANNER_NOT_EXIST);
        }else{
            resultModel.setBresult(true);
            resultModel.setObject(dbBanner);
        }
        return new JsonResult().Serailze(resultModel);
    }
}
